package com.yy.micro.magic.data.provider;

import com.yy.micro.magic.core.provider.impl.TriggerResourceProvider;
import com.yy.micro.magic.data.domain.DynamicTriggerVo;
import com.yy.micro.magic.data.service.trigger.MagicTriggerService;
import com.yy.micro.magic.ext.domain.filter.DynamicTrigger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@Component
public class DynamicTriggerResourceProvider extends TriggerResourceProvider {

    @Autowired
    private MagicTriggerService service;

    @Override
    public DynamicTrigger find(final String planCode, final String condition) {
        List<DynamicTriggerVo> vos= service.find(planCode);
        if (!CollectionUtils.isEmpty(vos)){
            Optional<DynamicTriggerVo> vo=  vos.stream().filter(v-> Pattern.matches(v.getTrigger(),condition)).findFirst();
            if (vo.isPresent()){
                return vo.get().toDomain();
            }
        }
        return null;
    }

}
